Ext.require([
    'Ext.form.field.ComboBox',
    'Ext.form.FieldSet',
    'Ext.tip.QuickTipManager',
    'Ext.toolbar.Paging',
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.state.*'
]);

Ext.onReady(function() {
    Ext.QuickTips.init();




    var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
        clicksToEdit: 1
    });
    
    var cbstore = new Ext.data.Store({
    	        singleton:true,
    	        autoDestroy:true,
    	        fields:[
    	            'id',
    	            'houseTypeName'
    	        ],
    	        proxy:{
    	            type:'ajax',
    	            url:jypath+'/estate/common/findHouseTypes',
    	            reader:{
                        type: 'json', 
                        root: 'rows',  //数据
                   },
                   extraParams:queryParams
    	        },
    	
    	        autoLoad:true
    });
    
    var propertyTypes= [
      			{"value":"1","text":"公寓"},
      			{"value":"2","text":"别墅"},
      			{"value":"3","text":"车位"}
      		];
    var propertyTypeStore = Ext.data.Store({
    	autoDestroy: true,
        fields :["value","text"],
        data : propertyTypes
    });
    
    
    var store = Ext.create('Ext.data.Store', {
        fields: ["id","combinedName","floor", "propertyType", "billingArea", "buildingArea", "innerArea", "sharingArea",
             "gardenArea", "basementArea", "freeArea","houseTypeId"],
        pageSize: 10,  //页容量5条数据
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        proxy: {
            type: 'ajax',
            url: jypath+'/estate/projectRoom/findBatchData',
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'rows',  //数据
                totalProperty: 'total' //数据总条数
            },
            extraParams:queryParams
        },
        autoLoad: true  //即时加载数据
    });
    var backProjectDetail=function(){
    	var url=jypath+'/estate/treeComponent/index?projectId='+projectId;
    	window.location.href=url;
    }
    var batchUpdate=function(){
    	/*getModifiedRecords*/
    	var records=store.getUpdatedRecords();
    	if(records.length<=0){
    		Ext.Msg.alert('提示', '您还未修改相关数据！');
    		return;
    	}
    	var jsonArray=[];
    	Ext.Array.each(records, function(name, index, countriesItSelf) {
    		jsonArray.push(Ext.JSON.encode(records[index].data));
    	});
    	var resultJson=Ext.JSON.encode(jsonArray);
    	console.log(resultJson);
    	Ext.Ajax.request({
    	    url: jypath+'/estate/projectRoom/saveBatchData',
    	    params: {
    	    	jsonArray: jsonArray
    	    },
    	    method: 'POST',
    	    success: function(response){
    	        var text = Ext.decode(response.responseText);
    	        console.log(text);
    	        if(text=="0"){
    	        	Ext.Msg.alert('提示', '数据修改成功！');
    	        	store.reload();
    	        }else if(text=="-1"){
    	        	Ext.Msg.alert('提示', '权限不足！');
    	        }else{
    	        	Ext.Msg.alert('提示', '数据修改失败！');
    	        }
    	    }
    	});
    }
    var grid = Ext.create('Ext.grid.Panel', {
        store: store,
        columnLines: true,
        columns: [{
            text: '房号',
            sortable : false,
            width:320,
            dataIndex: 'combinedName'
        },{
            text     : '楼层', 
            dataIndex: 'floor',
            editor: {
                xtype: 'numberfield',
                hideTrigger:true
            }
        },{
            text     : '户型',
            dataIndex: 'houseTypeId',
            editor:new Ext.form.field.ComboBox({
                queryMode:'local',
                id:'houseTypeId',
                valueField:'id',
                displayField:'houseTypeName',
                store:cbstore
            }),
            renderer: function(value,metadata,record){  
                var index = cbstore.find('id',value);  
                if(index!=-1){  
                    return cbstore.getAt(index).data.houseTypeName;  
                }  
                return value;  
            }
        },{
            text     : '房产类型',
            dataIndex: 'propertyType',
            editor:new Ext.form.field.ComboBox({
                queryMode:'local',
                id:'propertyType',
                valueField:'value',
                displayField:'text',
                store:propertyTypeStore,
                typeAhead: true
            }),
            renderer: function(value,metadata,record){ 
            	var valueName="";
                if(value=="1"){
                	valueName="公寓";
                }else if(value=="2"){
                	valueName="别墅";
                }else if(value=="3"){
                	valueName="车位";
                } 
                return valueName;  
            }
        },{
            text     : '套内面积(㎡)',
            dataIndex: 'innerArea',
            editor: {
                xtype: 'numberfield',
                hideTrigger:true
            }
        },{
            text     : '分摊面积(㎡)',
            dataIndex: 'sharingArea',
            editor: {
                xtype: 'numberfield',
                hideTrigger:true
            }
        },{
            text     : '房屋计算面积(㎡)',
            dataIndex: 'billingArea',
            editor: {
                xtype: 'numberfield',
                hideTrigger:true
            }
        },{
            text     : '房屋建筑面积(㎡)',
            dataIndex: 'buildingArea',
            editor: {
                xtype: 'numberfield',
                hideTrigger:true
            }
        },{
            text     : '花园面积(㎡)',
            dataIndex: 'gardenArea',
            editor: {
                xtype: 'numberfield'
            }
        },{
            text     : '地下室面积(㎡)',
            dataIndex: 'basementArea',
            editor: {
                xtype: 'numberfield',
                hideTrigger:true
            }
        },{
            text     : '赠送面积(㎡)',
            dataIndex: 'freeArea',
            editor: {
                xtype: 'numberfield',
                hideTrigger:true
            }
        }],
        selModel: {
            selType: 'cellmodel'
        },
        height: 500,
        title: '批量修改',
        renderTo: 'batchUpdateDiv',
        viewConfig: {
            stripeRows: true
        },
        plugins: [cellEditing],
        dockedItems: [{
            xtype: 'toolbar',
            dock: 'bottom',
            ui: 'footer',
            layout: {
                pack: 'center'
            },
            items: [{
                minWidth: 80,
                text: '保存',
                handler:batchUpdate
            },{
                minWidth: 80,
                text: '返回',
                handler:backProjectDetail
            }]
        },{  
            xtype: 'pagingtoolbar',  
            store: store,   // GridPanel中使用的数据  
            dock: 'bottom',  
            displayInfo: true,  
            emptyMsg:"没有数据",  
            height:50,  
            displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据",  
            store:store  
        }
        ]
    });
});

